home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 3 / BBS in a box - Trilogy III.iso / Files / Prog / T / TE32K Folder / TE32K Demo / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-02-12  |  20.9 KB  |  1,066 lines  |  [TEXT/KAHL]

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "TE32K.h"
  4.  
  5.  
  6. #define EQSTR    0
  7.  
  8.  
  9. #define    yesNoCancelDLOG    132
  10. #define    errorDLOG        256
  11.  
  12. #define    numMenus    4
  13.  
  14. #define    appleMenu    0
  15. #define    fileMenu    1
  16. #define    editMenu    2
  17. #define fontMenu    3
  18.  
  19. #define    appleID        128
  20. #define    fileID        129
  21. #define    editID        130
  22. #define fontID        131
  23.  
  24. #define    newCommand        1
  25. #define    openCommand        2
  26. #define insertCommand    3
  27. #define    closeCommand    4
  28. #define    saveCommand        5
  29. #define    saveAsCommand    6
  30. #define    quitCommand        8
  31.  
  32. #define    undoCommand        1
  33. #define    cutCommand        3
  34. #define    copyCommand        4
  35. #define pasteCommand    5
  36. #define    clearCommand    6
  37. #define    selectCommand    7
  38. #define    wrapCommand        9
  39.  
  40.  
  41.  
  42. char            alive;
  43. MenuHandle        myMenus[numMenus];
  44. char            tempString[256];
  45. WindowPtr        theWPtr = 0L;
  46. TE32KHandle        theTEH32K = 0L;
  47. ControlHandle    theVScroll = 0L, theHScroll = 0L;
  48. Cursor            editCursor,waitCursor;
  49. SFReply            mySFReply;
  50. char            changed;
  51. int                defaultFont,defaultFontSize;
  52. char            *theTestString = "This is terminated by a \\n character\n";
  53.  
  54. RestartProc()
  55. {
  56.     ExitToShell();
  57. }
  58.  
  59.  
  60.  
  61. main()
  62. {
  63. EventRecord        theEvent;
  64. WindowPtr        whichWindow;
  65. unsigned char    theChar;
  66.  
  67.     InitGraf(&thePort);
  68.     InitFonts();
  69.     FlushEvents(everyEvent,0);
  70.     InitWindows();
  71.     InitMenus();
  72.     TEInit();
  73.     InitDialogs(RestartProc);
  74.     InitCursor();
  75.     
  76.     TE32KInit();
  77.     
  78.     SetUpCursors();
  79.     
  80.     SetUpMenus();
  81.     
  82.     defaultFont = monaco;
  83.     defaultFontSize = 9;
  84.     MaintainFontMenu();
  85.     
  86.     FlushEvents(everyEvent,0);
  87.     
  88.     alive = TRUE;
  89.     
  90.     while (alive)
  91.     {
  92.         SystemTask();
  93.         MaintainCursor();
  94.         MaintainMenus();
  95.         DoIdle();
  96.         
  97.         if (GetNextEvent(everyEvent,&theEvent))
  98.         {
  99.             switch (theEvent.what)
  100.             {
  101.                 case mouseDown:
  102.                     DoMouseDown(&theEvent);
  103.                     break;
  104.                 
  105.                 case keyDown:
  106.                 case autoKey: 
  107.                     
  108.                     theChar = theEvent.message & charCodeMask;
  109.                     
  110.                     if (theEvent.modifiers & cmdKey)
  111.                         DoCommand(MenuKey((unsigned char) theChar));
  112.                     
  113.                     else
  114.                         DoKey((unsigned char) theChar);
  115.                     
  116.                     break;
  117.                     
  118.                 case activateEvt:
  119.                     if (IsOurWindow((WindowPtr) theEvent.message)) 
  120.                         DoActivateDeactivate((WindowPtr) theEvent.message,(char) (theEvent.modifiers & activeFlag));
  121.                     
  122.                     break;
  123.                     
  124.                 case updateEvt: 
  125.                     if (IsOurWindow((WindowPtr) theEvent.message)) 
  126.                         UpdateWindow((WindowPtr) theEvent.message);
  127.                     
  128.                     break;
  129.             }
  130.         }
  131.     }
  132. }
  133.  
  134.  
  135.  
  136.  
  137.  
  138. pascal void HiliteDefaultButton(theDPtr, whichItem)
  139. DialogPtr theDPtr; int whichItem;
  140. {
  141. int            type;
  142. Handle        theItem;
  143. Rect        theRect;
  144.  
  145.     GetDItem(theDPtr, ((DialogPeek) theDPtr)->aDefItem, &type, &theItem, &theRect);
  146.     PenNormal();
  147.     PenSize(3,3);
  148.     InsetRect(&theRect,-4,-4);
  149.     FrameRoundRect(&theRect,16,16);
  150.     PenSize(1,1);
  151. }
  152.  
  153.  
  154.  
  155. ErrorAlert(p0)
  156. char    *p0;
  157. {
  158. GrafPtr            oldPort;
  159. DialogPtr        theDPtr,tempDPtr;
  160. int                itemHit, type;
  161. Handle            theItem;
  162. Rect            theRect;
  163. EventRecord        theEvent;
  164.  
  165.     GetPort(&oldPort);
  166.     
  167.     InitCursor();
  168.     
  169.     if (!(theDPtr = GetNewDialog(errorDLOG, NULL,(WindowPtr) -1L)))
  170.     {
  171.         SysBeep(1);
  172.         ExitToShell();
  173.     }
  174.     
  175.     SetPort(theDPtr);
  176.     
  177.     CenterWindow(theDPtr);
  178.     ShowWindow(theDPtr);
  179.     
  180.     ((DialogPeek) theDPtr)->aDefItem = 1;
  181.     
  182.     GetDItem(theDPtr, 3, &type, &theItem, &theRect);
  183.     SetDItem(theDPtr, 3, type, HiliteDefaultButton, &theRect);
  184.     
  185.     GetDItem(theDPtr, 2, &type, &theItem, &theRect);
  186.     CtoPstr(p0);
  187.     SetIText(theItem, p0);
  188.     PtoCstr(p0);
  189.     
  190.     while (!GetNextEvent(updateMask,&theEvent));
  191.     
  192.     if (theEvent.message == (long) theDPtr)
  193.         DialogSelect(&theEvent,&tempDPtr,&itemHit);
  194.     
  195.     SysBeep(1);
  196.     
  197.     do 
  198.     {
  199.         itemHit = 0;
  200.         
  201.         while (!GetNextEvent(everyEvent,&theEvent));
  202.         
  203.         if (theEvent.what==keyDown || theEvent.what==autoKey)
  204.         {
  205.             if ((theEvent.message & charCodeMask)=='\r' || (theEvent.message & charCodeMask)==0x03)
  206.                 itemHit = ((DialogPeek) theDPtr)->aDefItem;
  207.             else
  208.                 SysBeep(1);
  209.         }
  210.         
  211.         else
  212.         {
  213.             tempDPtr = (DialogPtr) 0L;
  214.             
  215.             if (!IsDialogEvent(&theEvent) || !DialogSelect(&theEvent,&tempDPtr,&itemHit) || tempDPtr!=theDPtr)
  216.                 itemHit = 0;
  217.         }
  218.         
  219.     } while (itemHit!=1);
  220.     
  221.     DisposDialog(theDPtr);
  222.     SetPort(oldPort);
  223. }
  224.  
  225.  
  226.  
  227.  
  228.  
  229. SetUpMenus()
  230. {
  231. int            i,numDAs;
  232.  
  233.     myMenus[appleMenu] = GetMenu(appleID);
  234.     AddResMenu(myMenus[appleMenu],'DRVR');
  235.     myMenus[fileMenu] = GetMenu(fileID);
  236.     myMenus[editMenu] = GetMenu(editID);
  237.     myMenus[fontMenu] = GetMenu(fontID);
  238.     InsertResMenu(myMenus[fontMenu],'FONT',0);
  239.     
  240.     InsertMenu(myMenus[appleMenu],0);
  241.     InsertMenu(myMenus[fileMenu],0);
  242.     InsertMenu(myMenus[editMenu],0);
  243.     InsertMenu(myMenus[fontMenu],0);
  244.     
  245.     DrawMenuBar();
  246.     
  247.     numDAs = CountMItems(myMenus[appleMenu]);
  248.     
  249.     for (i=1;i<=numDAs;i++)
  250.         EnableItem(myMenus[appleMenu],i);
  251. }
  252.  
  253.  
  254.  
  255. MaintainMenus()
  256. {
  257. int        i,numItems;
  258.  
  259.     if (theWPtr && FrontWindow() == theWPtr)
  260.     {
  261.         DisableItem(myMenus[fileMenu],newCommand);
  262.         DisableItem(myMenus[fileMenu],openCommand);
  263.         EnableItem(myMenus[fileMenu],insertCommand);
  264.         EnableItem(myMenus[fileMenu],closeCommand);
  265.         
  266.         if (changed)
  267.             EnableItem(myMenus[fileMenu],saveCommand);
  268.         else
  269.             DisableItem(myMenus[fileMenu],saveCommand);
  270.         
  271.         EnableItem(myMenus[fileMenu],saveAsCommand);
  272.         
  273.         
  274.         DisableItem(myMenus[editMenu],undoCommand);
  275.         
  276.         if (theTEH32K)
  277.         {
  278.             if ((**theTEH32K).selStart == (**theTEH32K).selEnd)
  279.             {
  280.                 DisableItem(myMenus[editMenu],cutCommand);
  281.                 DisableItem(myMenus[editMenu],copyCommand);
  282.                 DisableItem(myMenus[editMenu],clearCommand);
  283.             }
  284.             
  285.             else
  286.             {
  287.                 EnableItem(myMenus[editMenu],cutCommand);
  288.                 EnableItem(myMenus[editMenu],copyCommand);
  289.                 EnableItem(myMenus[editMenu],clearCommand);
  290.             }
  291.             
  292.             if (TE32KGetScrapLen() > 0L)
  293.                 EnableItem(myMenus[editMenu],pasteCommand);
  294.             else
  295.                 DisableItem(myMenus[editMenu],pasteCommand);
  296.             
  297.             EnableItem(myMenus[editMenu],selectCommand);
  298.             EnableItem(myMenus[editMenu],wrapCommand);
  299.             
  300.             if (theTEH32K && !(**theTEH32K).crOnly)
  301.             {
  302.                 CheckItem(myMenus[editMenu],wrapCommand,TRUE);
  303.                 SetItemMark(myMenus[editMenu],wrapCommand,checkMark);
  304.             }
  305.                 
  306.             else
  307.                 CheckItem(myMenus[editMenu],wrapCommand,FALSE);
  308.         }
  309.         
  310.         
  311.         numItems = CountMItems(myMenus[fontMenu]);
  312.         
  313.         for (i=1;i<=numItems;i++)
  314.             EnableItem(myMenus[fontMenu],i);
  315.         
  316.         MaintainFontMenu();
  317.     }
  318.     
  319.     else
  320.     {
  321.         EnableItem(myMenus[fileMenu],newCommand);
  322.         EnableItem(myMenus[fileMenu],openCommand);
  323.         DisableItem(myMenus[fileMenu],insertCommand);
  324.         DisableItem(myMenus[fileMenu],closeCommand);
  325.         DisableItem(myMenus[fileMenu],saveCommand);
  326.         DisableItem(myMenus[fileMenu],saveAsCommand);
  327.         
  328.         
  329.         EnableItem(myMenus[editMenu],undoCommand);
  330.         EnableItem(myMenus[editMenu],cutCommand);
  331.         EnableItem(myMenus[editMenu],copyCommand);
  332.         EnableItem(myMenus[editMenu],pasteCommand);
  333.         EnableItem(myMenus[editMenu],clearCommand);
  334.         DisableItem(myMenus[editMenu],selectCommand);
  335.         DisableItem(myMenus[editMenu],wrapCommand);
  336.         CheckItem(myMenus[editMenu],wrapCommand,FALSE);
  337.         
  338.         numItems = CountMItems(myMenus[fontMenu]);
  339.         
  340.         for (i=1;i<=numItems;i++)
  341.         {
  342.             DisableItem(myMenus[fontMenu],i);
  343.             CheckItem(myMenus[fontMenu],i,FALSE);
  344.         }
  345.     }
  346.  
  347. }
  348.  
  349.  
  350. DoCommand(mResult)
  351. long mResult;
  352. {
  353. int            theItem,numItems,userChoice;
  354. WindowPeek     wPtr;
  355. GrafPtr        oldPort;
  356. SFReply        oldSFReply;
  357. Rect        tempRect;
  358.  
  359.  
  360.     theItem = LoWord(mResult);
  361.     
  362.     switch (HiWord(mResult)) 
  363.     {
  364.         case appleID:
  365.             if (theItem == 1)
  366.                 DoAboutBox(0);
  367.             
  368.             else
  369.             {
  370.                 GetPort(&oldPort);
  371.                 GetItem(myMenus[appleMenu], theItem, &tempString);
  372.                 OpenDeskAcc(&tempString);
  373.                 SetPort(oldPort);
  374.             }
  375.             
  376.             break;
  377.             
  378.         case fileID:
  379.             switch (theItem)
  380.             {
  381.                 case newCommand:
  382.                     if (!theTEH32K)
  383.                         DoShowWindow();
  384.                     else
  385.                         SysBeep(1);
  386.                     
  387.                     break;
  388.                 
  389.                 case openCommand:
  390.                     if (theTEH32K && DoCloseWindow(theWPtr))
  391.                     {
  392.                         DoShowWindow();
  393.                         
  394.                         if (OpenTextFile(theTEH32K,&mySFReply))
  395.                         {
  396.                             SetWTitle(theWPtr,mySFReply.fName);
  397.                             AdjustScrollBar();
  398.                             changed = FALSE;
  399.                         }
  400.                     }
  401.                     
  402.                     else if (!theTEH32K)
  403.                     {
  404.                         DoShowWindow();
  405.                         
  406.                         if (OpenTextFile(theTEH32K,&mySFReply))
  407.                         {
  408.                             SetWTitle(theWPtr,mySFReply.fName);
  409.                             AdjustScrollBar();
  410.                             changed = FALSE;
  411.                         }
  412.                     }
  413.                     
  414.                     break;
  415.                 
  416.                 case insertCommand:
  417.                     if (theTEH32K)
  418.                     {
  419.                         changed = InsertTextFile(theTEH32K);
  420.                         
  421.                         AdjustScrollBar();
  422.                     }
  423.                     
  424.                     break;
  425.                     
  426.                 case closeCommand:
  427.                     if (theTEH32K)
  428.                         DoCloseWindow(theWPtr);
  429.                     else
  430.                         SysBeep(1);
  431.                         
  432.                     break;
  433.                 
  434.                 case saveCommand:
  435.                     if (theTEH32K)
  436.                     {
  437.                         if (SaveTextFile(theTEH32K,&mySFReply))
  438.                         {
  439.                             SetWTitle(theWPtr,mySFReply.fName);
  440.                             changed = FALSE;
  441.                         }
  442.                     }
  443.                         
  444.                     else
  445.                         SysBeep(1);
  446.                     
  447.                     break;
  448.                 
  449.                 case saveAsCommand:
  450.                     if (theTEH32K)
  451.                     {
  452.                         oldSFReply = mySFReply;
  453.                         mySFReply.good = FALSE;
  454.                         
  455.                         if (SaveTextFile(theTEH32K,&mySFReply))
  456.                         {
  457.                             changed = FALSE;
  458.                             SetWTitle(theWPtr,mySFReply.fName);
  459.                         }
  460.                         else
  461.                             mySFReply = oldSFReply;
  462.                     }
  463.                         
  464.                     else
  465.                         SysBeep(1);
  466.                     
  467.                     break;
  468.                 
  469.                 case quitCommand:
  470.                     if (theTEH32K)
  471.                     {
  472.                         if (DoCloseWindow(theWPtr))
  473.                             alive = FALSE;
  474.                     }
  475.                     else
  476.                         alive = FALSE;
  477.                     
  478.                     break;
  479.             }
  480.             
  481.             break;
  482.         
  483.         case editID:
  484.             switch(theItem)
  485.             {
  486.                 case cutCommand:
  487.                     if (!DoCut())
  488.                         SystemEdit(theItem - 1);
  489.                     break;
  490.                 
  491.                 case copyCommand:
  492.                     if (!DoCopy())
  493.                         SystemEdit(theItem - 1);
  494.                     break;
  495.                 
  496.                 case pasteCommand:
  497.                     if (!DoPaste())
  498.                         SystemEdit(theItem - 1);
  499.                     
  500.                     break;
  501.                 
  502.                 case clearCommand:
  503.                     if (!DoClear())
  504.                         SystemEdit(theItem - 1);
  505.                     
  506.                     break;
  507.                 
  508.                 case selectCommand:
  509.                     if (!DoSelectAll())
  510.                         SystemEdit(theItem - 1);
  511.                     
  512.                     break;
  513.                 
  514.                 case wrapCommand:
  515.                     if (theWPtr && theWPtr == FrontWindow() && theTEH32K)
  516.                     {
  517.                         (**theTEH32K).crOnly = !(**theTEH32K).crOnly;
  518.                         
  519.                         TE32KCalText(theTEH32K);
  520.                         
  521.                         tempRect = theWPtr->portRect;
  522.                         GetPort(&oldPort);
  523.                         SetPort(theWPtr);
  524.                         InvalRect(&tempRect);
  525.                         SetPort(oldPort);
  526.                         
  527.                         AdjustForResizedWindow();
  528.                     }
  529.                     
  530.                     break;
  531.                 
  532.                 
  533.                 default:
  534.                     SystemEdit(theItem - 1);
  535.                     break;
  536.             }
  537.             
  538.             break;
  539.             
  540.         case fontID:
  541.             numItems = CountMItems(myMenus[fontMenu]);
  542.             
  543.             if (theItem <= numItems-7)
  544.             {
  545.                 GetItem(myMenus[fontMenu], theItem, &tempString);
  546.                 PtoCstr(tempString);
  547.                 DoFontSize(tempString,-1);
  548.             }
  549.             
  550.             else
  551.             {
  552.                 GetItem(myMenus[fontMenu], theItem, &tempString);
  553.                 PtoCstr(tempString);
  554.                 
  555.                 if (sscanf(tempString,"%d",&numItems)==1)
  556.                 {
  557.                     tempString[0] = 0;
  558.                     DoFontSize(tempString,numItems);
  559.                 }
  560.             }
  561.             
  562.             break;
  563.     }
  564.     
  565.     HiliteMenu(0);
  566. }
  567.  
  568.  
  569.  
  570.  
  571. OpenTextFile(theTEH,mySFReply)
  572. TE32KHandle    theTEH;SFReply    *mySFReply;
  573. {
  574. SFTypeList    mySFTypes;
  575. Point        theTopLeft;
  576. int            errCode,fRefNum;
  577. long        byteCount;
  578. Ptr            theText;
  579.  
  580.  
  581.     if (theTEH)
  582.     {
  583.         theTopLeft.h = screenBits.bounds.left + (screenBits.bounds.right - screenBits.bounds.left - 348)/2;;
  584.         theTopLeft.v = screenBits.bounds.top + 50;
  585.         
  586.         mySFTypes[0] = 'TEXT';
  587.         
  588.         InitCursor();
  589.         
  590.         SFGetFile(theTopLeft,"\Select Text File To Open:",0L,1,&mySFTypes,0L,mySFReply);
  591.         
  592.         if (mySFReply->good)
  593.         {
  594.             if ((errCode = FSOpen(mySFReply->fName,mySFReply->vRefNum,&fRefNum))!=noErr)
  595.             {
  596.                 mySFReply->good = FALSE;
  597.                 PtoCstr((char *) mySFReply->fName);
  598.                 sprintf(tempString,"OpenTextFile: FSOpen('%s') Error %d",mySFReply->fName,errCode);
  599.                 ErrorAlert(tempString);
  600.                 return(FALSE);
  601.             }
  602.             
  603.             if ((errCode = GetEOF(fRefNum,&byteCount))!=noErr)
  604.             {
  605.                 mySFReply->good = FALSE;
  606.                 FSClose(fRefNum);
  607.                 PtoCstr((char *) mySFReply->fName);
  608.                 sprintf(tempString,"OpenTextFile: GetEOF('%s',0) Error %d",mySFReply->fName,errCode);
  609.                 ErrorAlert(tempString);
  610.                 return(FALSE);
  611.             }
  612.             
  613.             if ((errCode = SetFPos(fRefNum,fsFromStart,0L))!=noErr)
  614.             {
  615.                 mySFReply->good = FALSE;
  616.                 FSClose(fRefNum);
  617.                 PtoCstr((char *) mySFReply->fName);
  618.                 sprintf(tempString,"OpenTextFile: SetFPos('%s',0) Error %d",mySFReply->fName,errCode);
  619.                 ErrorAlert(tempString);
  620.                 return(FALSE);
  621.             }
  622.             
  623.             theText = NewPtr(byteCount);
  624.             
  625.             if (StripAddress(theText)==0L || MemError())
  626.             {
  627.                 mySFReply->good = FALSE;
  628.                 FSClose(fRefNum);
  629.                 sprintf(tempString,"OpenTextFile: NewPtr(%ld) Error %d",byteCount,MemError());
  630.                 ErrorAlert(tempString);
  631.                 return(FALSE);
  632.             }
  633.             
  634.             SetCursor(&waitCursor);
  635.             
  636.             if ((errCode = FSRead(fRefNum,&byteCount,theText))!=noErr)
  637.             {
  638.                 InitCursor();
  639.                 mySFReply->good = FALSE;
  640.                 DisposPtr(theText);
  641.                 FSClose(fRefNum);
  642.                 PtoCstr((char *) mySFReply->fName);
  643.                 sprintf(tempString,"OpenTextFile: FSRead('%s') Error %d",mySFReply->fName,errCode);
  644.                 ErrorAlert(tempString);
  645.                 return(FALSE);
  646.             }
  647.             
  648.             if ((errCode = FSClose(fRefNum))!=noErr)
  649.             {
  650.                 InitCursor();
  651.                 mySFReply->good = FALSE;
  652.                 DisposPtr(theText);
  653.                 PtoCstr((char *) mySFReply->fName);
  654.                 sprintf(tempString,"OpenTextFile: FSClose('%s',0) Error %d",mySFReply->fName,errCode);
  655.                 ErrorAlert(tempString);
  656.                 return(FALSE);
  657.             }
  658.             
  659.             TE32KSetText(theText,byteCount,theTEH);
  660.             
  661.             DisposPtr(theText);
  662.             
  663.             InitCursor();
  664.             
  665.             return(TRUE);
  666.         }
  667.         
  668.         else
  669.             return(FALSE);
  670.     }
  671.     
  672.     else
  673.         return(FALSE);
  674. }
  675.  
  676.  
  677.  
  678. SaveTextFile(theTEH,mySFReply)
  679. TE32KHandle    theTEH;SFReply    *mySFReply;
  680. {
  681. Point        theTopLeft;
  682. int            errCode,fRefNum;
  683. long        byteCount;
  684. Handle        theText;
  685.  
  686.     if (theTEH)
  687.     {
  688.         theTopLeft.h = screenBits.bounds.left + (screenBits.bounds.right - screenBits.bounds.left - 304)/2;;
  689.         theTopLeft.v = screenBits.bounds.top + 50;
  690.         
  691.         InitCursor();
  692.         
  693.         if (!(mySFReply->good))
  694.             SFPutFile(theTopLeft,"\pSave Text File As:","\p",0L,mySFReply);
  695.         
  696.         if (mySFReply->good)
  697.         {
  698.             if ((errCode = FSOpen(mySFReply->fName,mySFReply->vRefNum,&fRefNum))!=noErr)
  699.             {
  700.                 if (errCode==fnfErr)
  701.                 {
  702.                     if ((errCode = Create(mySFReply->fName,mySFReply->vRefNum,'????','TEXT'))!=noErr)
  703.                     {
  704.                         mySFReply->good = FALSE;
  705.                         PtoCstr((char *) mySFReply->fName);
  706.                         sprintf(tempString,"SaveTextFile: Create('%s') Error %d",mySFReply->fName,errCode);
  707.                         ErrorAlert(tempString);
  708.                         return(FALSE);
  709.                     }
  710.                     
  711.                     errCode = FSOpen(mySFReply->fName,mySFReply->vRefNum,&fRefNum);
  712.                 }
  713.                 
  714.                 if (errCode != noErr)
  715.                 {
  716.                     mySFReply->good = FALSE;
  717.                     PtoCstr((char *) mySFReply->fName);
  718.                     sprintf(tempString,"SaveTextFile: FSOpen('%s') Error %d",mySFReply->fName,errCode);
  719.                     ErrorAlert(tempString);
  720.                     return(FALSE);
  721.                 }
  722.             }
  723.             
  724.             if ((errCode = SetEOF(fRefNum,0L))!=noErr)
  725.             {
  726.                 mySFReply->good = FALSE;
  727.                 FSClose(fRefNum);
  728.                 PtoCstr((char *) mySFReply->fName);
  729.                 sprintf(tempString,"SaveTextFile: SetEOF('%s',0) Error %d",mySFReply->fName,errCode);
  730.                 ErrorAlert(tempString);
  731.                 return(FALSE);
  732.             }
  733.             
  734.             if ((errCode = SetFPos(fRefNum,fsFromStart,0L))!=noErr)
  735.             {
  736.                 mySFReply->good = FALSE;
  737.                 FSClose(fRefNum);
  738.                 PtoCstr((char *) mySFReply->fName);
  739.                 sprintf(tempString,"SaveTextFile: SetFPos('%s',0) Error %d",mySFReply->fName,errCode);
  740.                 ErrorAlert(tempString);
  741.                 return(FALSE);
  742.             }
  743.             
  744.             theText = (**theTEH).hText;
  745.             byteCount = (**theTEH).teLength;
  746.             
  747.             HLock(theText);
  748.             
  749.             SetCursor(&waitCursor);
  750.             
  751.             if ((errCode = FSWrite(fRefNum,&byteCount,*theText))!=noErr)
  752.             {
  753.                 InitCursor();
  754.                 mySFReply->good = FALSE;
  755.                 HUnlock(theText);
  756.                 FSClose(fRefNum);
  757.                 PtoCstr((char *) mySFReply->fName);
  758.                 sprintf(tempString,"SaveTextFile: FSWrite('%s') Error %d",mySFReply->fName,errCode);
  759.                 ErrorAlert(tempString);
  760.                 return(FALSE);
  761.             }
  762.             
  763.             HUnlock(theText);
  764.             
  765.             if ((errCode = FSClose(fRefNum))!=noErr)
  766.             {
  767.                 InitCursor();
  768.                 mySFReply->good = FALSE;
  769.                 PtoCstr((char *) mySFReply->fName);
  770.                 sprintf(tempString,"SaveTextFile: FSClose('%s',0) Error %d",mySFReply->fName,errCode);
  771.                 ErrorAlert(tempString);
  772.                 return(FALSE);
  773.             }
  774.             
  775.             FlushVol("\p",mySFReply->vRefNum);
  776.             
  777.             InitCursor();
  778.             
  779.             return(TRUE);
  780.         }
  781.         
  782.         else
  783.             return(FALSE);
  784.     }
  785.     
  786.     else
  787.         return(FALSE);
  788. }
  789.  
  790.  
  791.  
  792.  
  793. YesNoCancel(thePrompt,theDefault)
  794. char    *thePrompt;int    theDefault;
  795. {
  796. DialogPtr        theDPtr,tempDPtr;
  797. int                itemHit, type;
  798. Handle            theItem;
  799. Rect            tempRect;
  800. GrafPtr            oldPort;
  801. char            tempString[256];
  802. EventRecord        theEvent;
  803.  
  804.     GetPort(&oldPort);
  805.     
  806.     InitCursor();
  807.     
  808.     theDPtr = GetNewDialog(yesNoCancelDLOG, 0L,(WindowPtr) -1L);
  809.     
  810.     CenterWindow(theDPtr);
  811.     ShowWindow(theDPtr);
  812.     
  813.     SetPort(theDPtr);
  814.     
  815.     ((DialogPeek) theDPtr)->aDefItem = theDefault;
  816.     
  817.     GetDItem(theDPtr, 4, &type, &theItem, &tempRect);
  818.     SetDItem(theDPtr, 4, type, HiliteDefaultButton, &tempRect);
  819.     
  820.     strcpy(tempString,thePrompt);
  821.     CtoPstr(tempString);
  822.     GetDItem(theDPtr, 5, &type, &theItem, &tempRect);
  823.     SetIText(theItem, tempString);
  824.     
  825.     if (GetNextEvent(updateMask,&theEvent) && theEvent.message == (long) theDPtr)
  826.         DialogSelect(&theEvent,&tempDPtr,&itemHit);
  827.     
  828.     SysBeep(1);
  829.     
  830.     do 
  831.     {
  832.         ModalDialog(0L,&itemHit);
  833.         
  834.     } while (!(itemHit>=1 && itemHit<=3));
  835.     
  836.     DisposDialog(theDPtr);
  837.     SetPort(oldPort);
  838.     
  839.     return(itemHit);
  840. }
  841.  
  842.  
  843.  
  844.  
  845. CenterWindow(wPtr)
  846. WindowPtr    wPtr;
  847. {
  848. int        screenWidth,screenHeight,windowWidth,windowHeight,left,top;
  849.  
  850.     if (wPtr == 0L)
  851.         return;
  852.     
  853.     screenWidth = screenBits.bounds.right - screenBits.bounds.left;
  854.     screenHeight = screenBits.bounds.bottom - screenBits.bounds.top - 20;
  855.     windowWidth = wPtr->portRect.right - wPtr->portRect.left;
  856.     windowHeight = wPtr->portRect.bottom - wPtr->portRect.top;
  857.     
  858.     left = screenBits.bounds.left + (screenWidth - windowWidth)/2;
  859.     top = screenBits.bounds.top + 20 + (screenHeight - windowHeight)/2;
  860.     
  861.     if (left < 0)
  862.         left = 0;
  863.     
  864.     if (top < 20)
  865.         top = 20;
  866.     
  867.     MoveWindow(wPtr,left,top,FALSE);
  868. }
  869.  
  870.  
  871.  
  872.  
  873. InsertTextFile(theTEH)
  874. TE32KHandle    theTEH;
  875. {
  876. SFReply        mySFReply;
  877. SFTypeList    mySFTypes;
  878. Point        theTopLeft;
  879. int            errCode,fRefNum;
  880. long        byteCount;
  881. Ptr            theText;
  882. char        tempString[256];
  883.  
  884.     if (theTEH)
  885.     {
  886.         theTopLeft.h = screenBits.bounds.left + (screenBits.bounds.right - screenBits.bounds.left - 348)/2;;
  887.         theTopLeft.v = screenBits.bounds.top + 50;
  888.         
  889.         mySFTypes[0] = 'TEXT';
  890.         
  891.         InitCursor();
  892.         
  893.         SFGetFile(theTopLeft,"\Select Text File To Insert:",0L,1,&mySFTypes,0L,&mySFReply);
  894.         
  895.         if (mySFReply.good)
  896.         {
  897.             if ((errCode = FSOpen(mySFReply.fName,mySFReply.vRefNum,&fRefNum))!=noErr)
  898.             {
  899.                 PtoCstr((char *) mySFReply.fName);
  900.                 sprintf(tempString,"InsertTextFile: FSOpen('%s') Error %d",mySFReply.fName,errCode);
  901.                 ErrorAlert(tempString);
  902.                 return(TRUE);
  903.             }
  904.             
  905.             if ((errCode = GetEOF(fRefNum,&byteCount))!=noErr)
  906.             {
  907.                 FSClose(fRefNum);
  908.                 PtoCstr((char *) mySFReply.fName);
  909.                 sprintf(tempString,"InsertTextFile: GetEOF('%s',0) Error %d",mySFReply.fName,errCode);
  910.                 ErrorAlert(tempString);
  911.                 return(TRUE);
  912.             }
  913.             
  914.             if ((errCode = SetFPos(fRefNum,fsFromStart,0L))!=noErr)
  915.             {
  916.                 FSClose(fRefNum);
  917.                 PtoCstr((char *) mySFReply.fName);
  918.                 sprintf(tempString,"InsertTextFile: SetFPos('%s',0) Error %d",mySFReply.fName,errCode);
  919.                 ErrorAlert(tempString);
  920.                 return(TRUE);
  921.             }
  922.             
  923.             theText = NewPtr(byteCount);
  924.             
  925.             if (StripAddress(theText)==0L || MemError())
  926.             {
  927.                 FSClose(fRefNum);
  928.                 sprintf(tempString,"InsertTextFile: NewPtr(%ld) Error %d",byteCount,MemError());
  929.                 ErrorAlert(tempString);
  930.                 return(TRUE);
  931.             }
  932.             
  933.             if ((errCode = FSRead(fRefNum,&byteCount,theText))!=noErr)
  934.             {
  935.                 DisposPtr(theText);
  936.                 FSClose(fRefNum);
  937.                 PtoCstr((char *) mySFReply.fName);
  938.                 sprintf(tempString,"InsertTextFile: FSRead('%s') Error %d",mySFReply.fName,errCode);
  939.                 ErrorAlert(tempString);
  940.                 return(TRUE);
  941.             }
  942.             
  943.             if ((errCode = FSClose(fRefNum))!=noErr)
  944.             {
  945.                 DisposPtr(theText);
  946.                 PtoCstr((char *) mySFReply.fName);
  947.                 sprintf(tempString,"InsertTextFile: FSClose('%s',0) Error %d",mySFReply.fName,errCode);
  948.                 ErrorAlert(tempString);
  949.                 return(TRUE);
  950.             }
  951.             
  952.             SetCursor(&waitCursor);
  953.             
  954.             TE32KInsert(theText,byteCount,theTEH);
  955.             
  956.             InitCursor();
  957.             
  958.             DisposPtr(theText);
  959.         }
  960.         
  961.         return(TRUE);
  962.     }
  963.     
  964.     else
  965.         return(FALSE);
  966. }
  967.  
  968.  
  969.  
  970.  
  971. DoFontSize(theFontName,theFontSize)
  972. char *theFontName;int    theFontSize;
  973. {
  974. WindowPtr    theWPtr;
  975. int            theFontNum;
  976. Rect        tempRect;
  977. GrafPtr        oldPort;
  978.     
  979.     if (theTEH32K)
  980.     {
  981.         if (theFontName[0])
  982.         {
  983.             CtoPstr(theFontName);
  984.             GetFNum(theFontName,&theFontNum);
  985.         }
  986.         else
  987.             theFontNum = -1;
  988.         
  989.         
  990.         if (theFontNum < 0)
  991.             theFontNum = defaultFont;
  992.         
  993.         if (theFontSize <= 0)
  994.             theFontSize = defaultFontSize;
  995.             
  996.         defaultFont = theFontNum;
  997.         defaultFontSize = theFontSize;
  998.         
  999.         TE32KSetFontStuff(theFontNum,0,srcCopy,theFontSize,theTEH32K);
  1000.         
  1001.         AdjustForResizedWindow();
  1002.         
  1003.         GetPort(&oldPort);
  1004.         SetPort((**theTEH32K).inPort);
  1005.         tempRect = (*(**theTEH32K).inPort).portRect;
  1006.         InvalRect(&tempRect);
  1007.         SetPort(oldPort);
  1008.         
  1009.         MaintainFontMenu();
  1010.     }
  1011. }
  1012.  
  1013.  
  1014.  
  1015.  
  1016.  
  1017. MaintainFontMenu()
  1018. {
  1019. int        i,numItems,tempSize;
  1020. char    theFontString[128];
  1021. char    tempString[256];
  1022.  
  1023.     numItems = CountMItems(myMenus[fontMenu]);
  1024.     
  1025.     GetFontName(defaultFont,theFontString);
  1026.     PtoCstr(theFontString);
  1027.     
  1028.     for (i=1;i<=numItems-7;i++)
  1029.     {
  1030.         GetItem(myMenus[fontMenu],i,&tempString);
  1031.         PtoCstr(tempString);
  1032.         
  1033.         if (strcmp(tempString,theFontString) == EQSTR)
  1034.         {
  1035.             CheckItem(myMenus[fontMenu],i,TRUE);
  1036.             SetItemMark(myMenus[fontMenu],i,checkMark);
  1037.         }
  1038.         else
  1039.             CheckItem(myMenus[fontMenu],i,FALSE);
  1040.     }
  1041.     
  1042.     
  1043.     sprintf(theFontString,"%d",defaultFontSize);
  1044.     
  1045.     for (i=numItems-6;i<=numItems;i++)
  1046.     {
  1047.         GetItem(myMenus[fontMenu],i,&tempString);
  1048.         PtoCstr(tempString);
  1049.         
  1050.         if (sscanf(tempString,"%d",&tempSize) != 1)
  1051.             tempSize = -1;
  1052.         
  1053.         if (strcmp(tempString,theFontString) == EQSTR)
  1054.         {
  1055.             CheckItem(myMenus[fontMenu],i,TRUE);
  1056.             SetItemMark(myMenus[fontMenu],i,checkMark);
  1057.         }
  1058.         else
  1059.             CheckItem(myMenus[fontMenu],i,FALSE);
  1060.         
  1061.         if (RealFont(defaultFont,tempSize))
  1062.             SetItemStyle(myMenus[fontMenu],i,outline);
  1063.         else
  1064.             SetItemStyle(myMenus[fontMenu],i,0);
  1065.     }
  1066. }